import numpy as np
import time

MaxNumber = -2 ** 31
MinNumber = 2 ** 31 - 1


def getMax_MininTwo(arr: list, left: int, right: int):
    """
    两数或一个数的处理
    :param arr:
    :param left:
    :param right:
    :return:
    """
    global MaxNumber, MinNumber
    if arr[left] < arr[right]:
        if MaxNumber < arr[right]:
            MaxNumber = arr[right]
        if MinNumber > arr[left]:
            MinNumber = arr[left]
    else:
        if MaxNumber < arr[left]:
            MaxNumber = arr[left]
        if MinNumber > arr[right]:
            MinNumber = arr[right]


def getMax_Min(arr, left: int, right: int):
    global MaxNumber, MinNumber
    if right - left <= 1:
        getMax_MininTwo(arr, left, right)
    else:
        mid = (right + left) // 2
        # 递归调用 求出左侧的最大值最小值
        getMax_Min(arr, left, mid)
        # 递归调用  求出右侧的最小值
        getMax_Min(arr, mid, right)


def cmpTwpnumbers(arry: list, start: int, end: int) -> list:
    """
    两数或一个数的处理
    :param arry:
    :param start:
    :param end:
    :return:
    """
    max, min = 0, 0
    if start < end:
        if arry[start] < arry[end]:
            max = arry[end]
            min = arry[start]
        else:
            max = arry[start]
            min = arry[end]
    else:
        max, min = arry[start], arry[end]

    return [max, min]


def mergeFind(arry: list, start: int, end: int) -> list:
    """
    递归分治查找
    :rtype: object
    """
    max, min = 0, 0
    if end - start > 2:
        mid = (end + start) // 2
        info1 = mergeFind(arry, start, mid)
        info2 = mergeFind(arry, mid + 1, end)
        if info1[0] >= info2[0]:
            max = info1[0]
        else:
            max = info2[0]
        if info1[1] <= info2[1]:
            min = info1[1]
        else:
            min = info2[1]
    else:
        info = cmpTwpnumbers(arry, start, end)
        max, min = info[0], info[1]

    return [max, min]


def test():
    count = input("请输入产生数组元素个数")
    l = np.random.randint(0, 2 ** 31 - 1, int(count)).tolist()
    # time.clock()在3.8之后被替换为以下
    time1 = time.perf_counter()
    print(l)
    start, end = [0, len(l) - 1]
    print(mergeFind(l, start, end))
    l.sort(reverse=True)
    print([l[0], l[9]])
    time2 = time.perf_counter()
    print("消耗时间 ： %f s" % (time2 - time1))


def main():
    count = input("请输入产生数组元素个数")
    l = np.random.randint(0, 2 ** 31 - 1, int(count)).tolist()
    time1 = time.perf_counter()
    print(l)
    start, end = int(0), len(l) - 1
    getMax_Min(l, start, end)
    print([MaxNumber, MinNumber])
    l.sort(reverse=True)
    print([l[0], l[end]])
    time2 = time.perf_counter()
    print("消耗时间 ： %f s" % (time2 - time1))


if __name__ == '__main__':
    main()
    # test()
